const path = require("path");
const { name } = require("./package");
const packageName = require("./package.json").name;
const webpack = require("webpack");

function resolve(dir) {
  return path.join(__dirname, dir);
}

const port = 7016;

const publicPath =
  process.env.NODE_ENV === "production"
    ? "https://qiankun.umijs.org/"
    : `http://localhost:${port}`;

module.exports = {
  outputDir: "dist",
  assetsDir: "static",
  filenameHashing: true,
  devServer: {
    hot: true,
    disableHostCheck: true,
    port,
    overlay: {
      warnings: false,
      errors: true,
    },
    headers: {
      "Access-Control-Allow-Origin": "*",
    },
    // 跨域
    proxy: {
      // 只要axios请求中带有/api的url,就会触发代理机制
      "/asid/api": {
        // 目标路径：target(我们要代理请求的地址)
        target: "http://172.28.72.73:8000/asid/api",
        // 允许跨域
        changOrigin: true,
        // 重写路径 api代替了目标路径
        pathRewrite: {
          "^/asid/api": "",
        },
      },
    },
  },
  chainWebpack: (config) => {
    config.module
      .rule("fonts")
      .test(/.(ttf|otf|eot|woff|woff2)$/)
      .use("url-loader")
      .loader("url-loader")
      .tap((options) => {
        options = {
          // limit: 10000,
          name: "/static/fonts/[name].[ext]",
        };
        return options;
      });
  },
  // 自定义webpack配置
  configureWebpack: {
    resolve: {
      alias: {
        "@": resolve("src"),
      },
    },
    // output: {
    //     library: `${name}-[name]`,
    //     libraryTarget: "umd",
    //     jsonpFunction: `webpackJsonp_${name}`,
    // },
    output: {
      library: `${packageName}-[name]`,
      libraryTarget: "umd",
      jsonpFunction: `webpackJsonp_${packageName}`,
    },
    plugins: [
      new webpack.DefinePlugin({
        "process.argv": JSON.stringify(process.argv),
      }),
    ],
  },
};
